package com.leantech.android.browser.ui

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import com.cscj.android.utils.LOG
import com.leantech.android.browser.BuildConfig
import com.leantech.android.browser.databinding.ActivityCodeScanBinding
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.toast
import com.leantech.android.qrcode.core.BGAQRCodeUtil
import com.leantech.android.qrcode.core.QRCodeView
import java.io.InputStream

class CodeScanActivity : BaseActivity(), QRCodeView.Delegate {

    private lateinit var binding: ActivityCodeScanBinding

    companion object {
        const val ResultExtraText = "scannedText"

        fun getBitmapFromUri(context: Context, imageUri: Uri): Bitmap? {
            var is1: InputStream? = null
            try {
                is1 = context.contentResolver.openInputStream(imageUri)
                return BitmapFactory.decodeStream(is1)
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                is1?.close()
            }
            return null
        }

    }

    private val pickMedia =
        registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
            LOG.d("scan", "uri $uri")
            binding.zxingview.startSpotAndShowRect()
            if (uri != null) {
                val bm = getBitmapFromUri(this, uri)
                if (bm != null) {
                    binding.zxingview.decodeQRCode(bm)
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        BGAQRCodeUtil.setDebug(BuildConfig.DEBUG)
        binding = ActivityCodeScanBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        binding.zxingview.setDelegate(this)
        binding.btnPickPhoto.onThrottleClick {
            binding.zxingview.stopSpotAndHiddenRect()
            pickMedia.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
        }
    }

    override fun onStart() {
        super.onStart()
        binding.zxingview.startCamera()
        binding.zxingview.startSpotAndShowRect()
    }

    override fun onStop() {
        super.onStop()
        binding.zxingview.stopCamera()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.zxingview.onDestroy()
    }

    override fun onScanQRCodeSuccess(result: String?) {
        if (result.isNullOrBlank()) {
            toast("未发现二维码")
            return
        }
        setResult(RESULT_OK, Intent().apply {
            putExtra(ResultExtraText, result)
        })
        finish()
    }

    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
        val tipText = binding.zxingview.scanBoxView.tipText
        if (tipText.isNullOrBlank()) {
            binding.zxingview.scanBoxView.tipText = "请对准二维码"
        }
    }

    override fun onScanQRCodeOpenCameraError() {

    }

}